package indi.gao;

import lombok.Data;

import java.util.*;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2020$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2020$-04$-02$</P>
 * <P>@version 1.0</P>
 */
public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet<Apple> objects = new TreeSet<>(new MyComparetor());
        Apple apple1 = new Apple(100, "1");
        Apple apple2 = new Apple(2, "2");
        Apple apple3 = new Apple(2, "3");
        Apple apple5 = new Apple(2, "3");
        objects.add(apple3);
        objects.add(apple2);
        objects.add(apple1);
        objects.add(apple5);
        soutSet(objects);
        ArrayList<Apple> apples = new ArrayList<>(objects);
        soutList(apples);

        System.out.println("第一个元素是:" + objects.first() + "最后一个元素是：" + objects.last());
    }

    private static void soutSet(Set<Apple> set) {
        set.forEach(System.out::println);
    }

    private static void soutList(List<Apple> list) {
        list.forEach(System.out::println);
    }
}

@Data
class Apple {
    private Integer value;
    private String Name;

    Apple(Integer value, String name) {
        this.value = value;
        Name = name;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "value=" + value +
                ", Name='" + Name + '\'' +
                '}';
    }
}

class MyComparetor implements Comparator<Apple> {

    /**
     * @param obj1 the first object to compare
     * @param obj2 the second object to compare
     * @return negative if obj1 is less, positive if greater, zero if equal
     * 如果返回零进行覆盖操作
     * 大于零往前边插入
     * 小于零往后面插入
     */
    @Override
    public int compare(Apple o1, Apple o2) {
        int result = o1.getValue() - o2.getValue();
        if (result == 0) {
            result = 1;
        }
        return result;
    }
}
